package timing.ukulele.storage.service.minio;

import org.springframework.stereotype.Service;
import timing.ukulele.storage.model.MetadataInfoModel;
import timing.ukulele.storage.service.MetadataService;
import timing.ukulele.storage.service.NodeService;
import timing.ukulele.storage.vo.file.CompleteResultVO;
import timing.ukulele.storage.vo.file.FileCheckResultVO;
import timing.ukulele.storage.vo.file.FileMetadataInfoVO;

import java.util.List;

/**
 * 存储组件Service层公共方法实现类
 */
@Service
public class FileService {

    /**
     * 存储引擎Service接口定义
     */
    private final StorageEngineService storageEngineService;

    /**
     * 文件元数据服务接口定义
     */
    private final MetadataService fileMetadataService;

    /**
     * 构造方法
     *
     * @param storageEngineService 存储引擎Service接口定义
     * @param metadataService      文件元数据服务接口定义
     */
    public FileService(StorageEngineService storageEngineService, MetadataService metadataService) {
        this.storageEngineService = storageEngineService;
        this.fileMetadataService = metadataService;
    }

    /**
     * 上传任务初始化
     *
     * @param fileMd5      文件md5
     * @param fullFileName 文件名（含扩展名）
     * @param fileSize     文件长度
     * @param userId       用户编号
     * @return {@link FileCheckResultVO}
     */
    public FileCheckResultVO init(String fileMd5, String fullFileName, long fileSize, String userId, String team) {
        FileCheckResultVO resultVo = storageEngineService.init(fileMd5, fullFileName, fileSize, userId, team);
        if (resultVo != null) {
            for (FileCheckResultVO.Part part : resultVo.getPartList()) {
                part.setUrl(part.getUrl());
            }
        }

        return resultVo;
    }

    /**
     * 合并已分块的文件
     *
     * @param fileKey     文件关键
     * @param partMd5List 文件分块md5列表
     * @param userId      用户编号
     * @return {@link CompleteResultVO}
     */
    public CompleteResultVO complete(String fileKey, List<String> partMd5List, String userId, String team, String parentId) {
        CompleteResultVO completeResultVo = storageEngineService.complete(fileKey, partMd5List, userId, parentId, team);
        if (completeResultVo != null) {
            for (FileCheckResultVO.Part part : completeResultVo.getPartList()) {
                part.setUrl(part.getUrl());
            }
        }
        return completeResultVo;
    }

    /**
     * 查询元数据信息
     *
     * @param fileMetadataInfo 查询入参
     * @return 文件元数据信息集合
     */
    public List<FileMetadataInfoVO> list(MetadataInfoModel fileMetadataInfo) {
        // 列表查询，取得全部符合条件的数据
        return fileMetadataService.list(fileMetadataInfo);
    }
}
